home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / satan-1.1.1 / perl / config.pl next >
Text File  |  1995-04-10  |  2KB  |  67 lines

  1. #
  2. # rewrite satan.cf after the user changes it.
  3. #
  4. # suck in the changes, then just cycle through each line of the .cf file.
  5. # if there is a match, put the new value in there.
  6. #
  7. sub write_config_file {
  8. local($new_values) = @_;
  9. local(%new_values, $variable, $old_variable, $old_value);
  10.  
  11. #
  12. # split the strings into something easier to handle
  13. for ( split(/\n/, $new_values) ) {
  14.     next if !$_;
  15.  
  16.     ($variable, $value) = split(/=/, $_);
  17.  
  18.     # need to stick a dollar sign in front of var
  19.     $variable = "\$" . "$variable";
  20.     # and quotes around non-numbers
  21.     if ($value !~ /^\d+$/) { $value = "\"$value\""; }
  22.  
  23.     $new_values{$variable} = $value;
  24.     }
  25.  
  26. #
  27. # open the config and the scratch file
  28. #
  29. die "Can't read $SATAN_CF file!\n" unless open(CF, "$SATAN_CF");
  30. die "Can't write $SATAN_CF.new file!\n" unless open(CFN, ">$SATAN_CF.new");
  31.  
  32. while (<CF>) {
  33.     # punt if the going gets too tough...
  34.     if (!/^\$/) {
  35.         print CFN $_;
  36.         next;
  37.         }
  38.     
  39.     chop;
  40.     ($old_variable, $old_value) = split(/=\s+/, $_);
  41.  
  42.     # kill spaces and semicolons
  43.     $old_variable =~ s/\s//g;
  44.     $old_value =~ s/;//g;
  45.  
  46.     # suck in the lines, compare them to each of the vars gotten from user
  47.     for $variable (keys %new_values) {
  48.         if ($variable eq $old_variable) {
  49.             $old_value = $new_values{$variable};
  50.             }
  51.         }
  52.  
  53.     print "CF: $_ ($old_variable, $old_value)\n";
  54.     print CFN "$old_variable = $old_value;\n";
  55.     }
  56.  
  57. close(CF);
  58. close(CFN);
  59.  
  60. # move the evidence to where it belongs... old to .old, new to .cf:
  61. system("mv $SATAN_CF $SATAN_CF.old");
  62. system("mv $SATAN_CF.new $SATAN_CF");
  63.  
  64. }
  65.  
  66. 1;
  67.